package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.GoodsService;
import com.dongdongshop.service.TbSpecificationService;
import com.dongdongshop.service.TbTypeTemplateService;
import com.dongdongshop.vo.SpecVO;
import com.dongdongshop.vo.TemplateVO;
import io.netty.handler.codec.compression.GzipOptions;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("goods")
public class GoodsController {
    @Reference
    private GoodsService goodsService;
    @Reference
    private TbTypeTemplateService tbTypeTemplateService;
    @Reference
    private TbSpecificationService tbSpecificationService;
    @RequestMapping("addAllInfo")
    @ResponseBody
    public Result addAllInfo(TbGoods tbGoods, TbGoodsDesc tbGoodsDesc, String propertys, String thp1, String thp2){
//        TbGoods tbGoods, TbGoodsDesc tbGoodsDesc, BigDecimal[] arrPrice, Integer[] arrValue, String[] arrTitle
//        TbGoods tbGoods, TbGoodsDesc tbGoodsDesc, String propertys, String thp1, String thp2
        List<TbItem> itemList = JSONObject.parseArray(propertys, TbItem.class);
        tbGoods.setAuditStatus("0");
        tbGoods.setIsMarketable("1");
        Subject subject = SecurityUtils.getSubject();
        //此方法就是将session里面的对象取出来
        TbSeller  user = (TbSeller) subject.getPrincipal();
        tbGoods.setSellerId(user.getSellerId());
        int i = goodsService.addAllInfo(tbGoods, tbGoodsDesc, itemList, user.getName(), thp1, thp2);

        return Result.response(ResultEnum.SUCCESS);
    }
    @RequestMapping("getTemplateById")
    @ResponseBody
    public Result getTemplateMapper(Long typeId){
        TbTypeTemplate typeTemplate = tbTypeTemplateService.getTemplateMapper(typeId);
        TemplateVO templateVO = new TemplateVO();//通过typeId 查询到相对应的
        templateVO.setTypeTemplate(typeTemplate);
        List<Map> templates = JSONObject.parseArray(typeTemplate.getSpecIds(), Map.class);
        List<TbSpecification> tbSpecificationList = new ArrayList<>();
        for (Map map: templates) {
            Integer id =(Integer) map.get("id");
            String text = (String) map.get("text");
            TbSpecification spec = new TbSpecification();
            spec.setId(id.longValue());
            spec.setSpecName(text);
            tbSpecificationList.add(spec);
        }
        List<SpecVO> specVOList = new ArrayList<>();
        for (TbSpecification tbspec: tbSpecificationList) {
            Long id = tbspec.getId();
           SpecVO specVO = tbSpecificationService.toUpdate(id);
           specVOList.add(specVO);
        }
        templateVO.setSpecVOList(specVOList);

        return Result.response(ResultEnum.SUCCESS).setData(templateVO);
    }
}
